{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from osgeo import gdal\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 影像读入函数\n",
    "gdal.open()\n",
    ".RasterXSize #列数\n",
    ".RasterYSize #行数\n",
    ".RasterCount #波段数\n",
    ".GetRasterBand(1).ReadAsArray() #波段1数据\n",
    ".GetGeotransform() #仿射矩阵\n",
    ".GetProjection() #投影信息\n",
    ".GetRasterBand()\n",
    ".ReadAsArray(),波段读出为np.array()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "col,row,bands: 2930 2852 7\n"
     ]
    }
   ],
   "source": [
    "path_img=\"D:\\Lenovo\\Desktop\\云南大学\\开源地理信息系统\\Chapter5_data\\data2.tif\"\n",
    "dest=gdal.Open(path_img)\n",
    "x_size,y_size,num_band=dest.RasterXSize,dest.RasterYSize,dest.RasterCount\n",
    "print(\"col,row,bands:\",x_size,y_size,num_band)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2852, 2930)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "band=dest.GetRasterBand(1)\n",
    "band_array=band.ReadAsArray()\n",
    "band_array.shape\n",
    "#x方向为列数，y方向为行数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "geo_trans: (232935.0, 30.0, 0.0, 2812155.0, 0.0, -30.0)\n"
     ]
    }
   ],
   "source": [
    "geo_trans=dest.GetGeoTransform()\n",
    "print(\"geo_trans:\",geo_trans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x_size:2930,y_size:2852,numband:7\n",
      "shape of array: (2852, 2930)\n"
     ]
    }
   ],
   "source": [
    "proj=dest.GetProjection()\n",
    "x_size,y_size,num_band=dest.RasterXSize,dest.RasterYSize,dest.RasterCount\n",
    "print('x_size:%.0f,y_size:%.0f,numband:%.0f'%(x_size,y_size,num_band))\n",
    "band1=dest.GetRasterBand(1)\n",
    "band1_array=band1.ReadAsArray()\n",
    "print('shape of array:',band1_array.shape)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
